package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.ModifyCardNickNameRequest;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.mvp.contract.IModifyCardNickNameContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;

public class ModifyCardNickNamePresenter  extends IBasePresenter
        implements IModifyCardNickNameContact.IModifyCardNickNamePresenter<IModifyCardNickNameContact.IModifyCardNickNameView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public ModifyCardNickNamePresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void modifyCardNickName(String card_no, String card_nick_name) {
        ModifyCardNickNameRequest modifyCardNickNameRequest = new ModifyCardNickNameRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        modifyCardNickNameRequest.setAccess_token(access_token);
        modifyCardNickNameRequest.setCard_nick_name(card_nick_name);
        modifyCardNickNameRequest.setCard_no(card_no);
        mNetworkDateSource.modifyCardNickName(modifyCardNickNameRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                if (mView != null) {
                    ((IModifyCardNickNameContact.IModifyCardNickNameView) mView).modifySuccess();
                }

            }

            @Override
            protected void onNext(IbaseBean response) {
                ((IModifyCardNickNameContact.IModifyCardNickNameView) mView).modifyFaile(response.getMessage());
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                ((IModifyCardNickNameContact.IModifyCardNickNameView) mView).modifyFaile(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IModifyCardNickNameContact.IModifyCardNickNameView) mView).modifyFaile(throwable.getMessage());
            }
        });
    }
}
